package cn.deschen.eshop.user.service.impl;

import cn.deschen.eshop.common.enums.GlobalEnum;
import cn.deschen.eshop.common.exception.GlobalException;
import cn.deschen.eshop.generator.mapper.UmsRoleMapper;
import cn.deschen.eshop.generator.model.UmsRole;
import cn.deschen.eshop.generator.model.UmsRoleExample;
import cn.deschen.eshop.user.service.UmsRoleCacheService;
import cn.deschen.eshop.user.service.UmsRoleService;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.channels.Pipe;
import java.util.List;

/**
 * @Author hanbin_chen
 * @Description 后台角色管理Service
 * @DateTime 2021/2/5 15:31
 * @Version V1.0.0
 */
@Service
@Slf4j
public class UmsRoleServiceImpl implements UmsRoleService {

    @Autowired
    private UmsRoleMapper roleMapper;

    @Autowired
    private UmsRoleCacheService roleCacheService;

    @Override
    public void createRole(UmsRole role) {
        int result = roleMapper.insert(role);
        if (result == 0) {
            log.error("【用户模块】创建角色信息失败 role = {}", role);
            throw new GlobalException(GlobalEnum.OPERATION_ERROR, "创建角色信息失败");
        }
        // 删除缓存信息
        roleCacheService.delRoleList();
    }

    @Override
    public void updateRoleById(UmsRole role) {
        int result = roleMapper.updateByPrimaryKey(role);
        if (result == 0) {
            log.error("【用户模块】更新角色信息失败 role = {}", role);
            throw new GlobalException(GlobalEnum.OPERATION_ERROR, "更新角色信息失败");
        }
        // 删除缓存信息
        roleCacheService.delRoleList();
    }

    @Override
    public void deleteRoleById(Long roleId) {
        int result = roleMapper.deleteByPrimaryKey(roleId);
        if (result == 0) {
            log.error("【用户模块】删除角色信息失败 id = {}", roleId);
            throw new GlobalException(GlobalEnum.OPERATION_ERROR, "删除角色信息失败");
        }
        // 删除缓存信息
        roleCacheService.delRoleList();
    }

    @Override
    public UmsRole getRoleById(Long roleId) {
        // 从缓存中获取角色信息
        UmsRole role = roleCacheService.getRoleById(roleId);
        if (null == role) {
            // 缓存为空，则从数据库获取
            role = roleMapper.selectByPrimaryKey(roleId);
        }
        // 判断角色是否存在
        if (null == role) {
            log.error("【用户模块】查询角色信息不存在 id = {}", roleId);
            throw new GlobalException(GlobalEnum.OPERATION_ERROR, "查询角色信息不存在");
        }
        // 缓存角色
        roleCacheService.setRoleById(roleId, role);

        return role;
    }

    @Override
    public List<UmsRole> getRoleList() {
        // 从缓存中获取角色信息
        List<UmsRole> roleList = roleCacheService.getRoleList();
        if (CollectionUtil.isEmpty(roleList)) {
            // 缓存为空，则从数据库获取
            roleList = roleMapper.selectByExample(null);
        }
        // 缓存角色
        roleCacheService.getRoleList();

        return roleList;
    }

    @Override
    public List<UmsRole> getRoleListByName(String name, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageSize, pageNum);
        UmsRoleExample roleExample = new UmsRoleExample();
        roleExample.createCriteria()
                .andNameLike("%" + name + "%");
        List<UmsRole> roleList = roleMapper.selectByExample(roleExample);

        return roleList;
    }
}
